/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sh.ndt.user.dao;

import com.sh.ndt.common.DBObjects;
import com.sh.ndt.common.dao.BaseDAOImpl;
import com.sh.ndt.common.dao.ExtractDataRows;
import com.sh.ndt.exam.entity.Exam;
import com.sh.ndt.exam.entity.ExamScheduleDetail;
import com.sh.ndt.exam.entity.ExamStatistics;
import com.sh.ndt.exam.mapper.ExamScheduleMapper;
import com.sh.ndt.exam.mapper.TestHistoryMapper;
import com.sh.ndt.exam.mapper.TestStatsMapper;
import com.sh.ndt.exception.DAOException;
import com.sh.ndt.user.entity.UserDashboardDetail;
import com.sh.ndt.user.entity.UsersTestHistory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 *
 * @author Prashanth
 */
public class UserEntitlementDAOImpl extends JdbcDaoSupport implements UserEntitlementDAO {

    private static final Logger LOGGER = Logger.getLogger(UserEntitlementDAOImpl.class);

    private UserEntitlementDAOImpl(){
        LOGGER.debug("****************** UserEntitlementDAOImpl Initialized **");
    }   
    
    @Override
    public UserDashboardDetail getUserDashboardDetail(int userId) throws DAOException {

        SqlParameter sqlParam[] = new SqlParameter[7];
        sqlParam[0] = new SqlParameter("in_user_id", Types.INTEGER);
        sqlParam[1] = new SqlReturnResultSet("exam_schedule_list", new ExamScheduleMapper());
        sqlParam[2] = new SqlReturnResultSet("test_history_list", new TestHistoryMapper());
        sqlParam[3] = new SqlOutParameter("OUT_TEST_ATTEMPT_COUNT", Types.INTEGER);
        sqlParam[4] = new SqlOutParameter("OUT_CREDIT_POINTS", Types.INTEGER);
        sqlParam[5] = new SqlOutParameter("OUT_R3_STATUS", Types.INTEGER); // 0 - Do not permit, 1 - Permit
        sqlParam[6] = new SqlReturnResultSet("test_statistical_list", new TestStatsMapper());

        ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_GET_DASHBOARD_DETAIL, sqlParam);
        HashMap inputs = new HashMap();
        inputs.put("in_user_id", userId);

        Map results = rows.executeStoredProcedure(inputs);

        UserDashboardDetail userDashboard = new UserDashboardDetail();

        
        if(results.get("OUT_CREDIT_POINTS") !=null)
            userDashboard.setCreditedPoints((Integer) results.get("OUT_CREDIT_POINTS"));
        if(results.get("OUT_TEST_ATTEMPT_COUNT") !=null)
            userDashboard.setMockTestAttepts((Integer) results.get("OUT_TEST_ATTEMPT_COUNT"));
        if(results.get("OUT_R3_STATUS") !=null)
            userDashboard.setR2status((Integer) results.get("OUT_R3_STATUS"));
        
        userDashboard.setExamDetail(new Exam());
        userDashboard.setUsersTestHistoryList((List<UsersTestHistory>) results.get("test_history_list"));
        //userDashboard.setCourseList((List<CourseParam>) results.get("course_list"));
        userDashboard.setExamScheduleList((List<ExamScheduleDetail>) results.get("exam_schedule_list"));
        userDashboard.setTestStatsList((List<ExamStatistics>) results.get("test_statistical_list"));
        
        return userDashboard;

    }

}
